/*
 * @Author: DaTree
 * @Date: 2020-11-18 13:11:30
 * @LastEditTime: 2020-11-19 22:59:50
 * @LastEditors: DaTree
 * @Description: DaTreeEdit
 * @FilePath: \services\src\controller\socket\socket-control.js
 * @天将降大任于是人也，必先苦其心志，劳其筋骨，饿其体肤，空乏其身，行拂乱其所为。 ——《孟子•告子下》
 * @symbol_custom_string_obkoro1_copyright: Copyright ${now_year} DaTree
 */
import SSH2, { SSH2ServerConfig } from '../../utils/ssh2'
import path from 'path'
import Response, { FailResponse } from '../response'

const sshClients = {

}
const control = {
  /**
   * 初始化linux连接
   * @param {SSH2ServerConfig} data 
   * @param {Function} then 
   */
  init (data, then) {
    try {
      // console.log(data)
      const ssh2 = new SSH2(new SSH2ServerConfig(data))
      const serverId = 'server-' + new Date().valueOf()
      sshClients[serverId] = ssh2
      then(new Response({ data, serverId }))
    } catch (error) {
      then(new FailResponse({data,error}))
    }
  },
  /**
   * 打开ssh连接
   * @param {String} data serverId
   * @param {Function} then 
   */
  connect (data, then) {
    try {
      const client = sshClients[data.serverId]
      // console.log(client)
      if (client) {
        client.connect((result) => {
          if (result) {
            then(new Response({ data }))
          } else {
            then(new FailResponse({data,result}))
          }
        })
      } else {
        then(new FailResponse({data,result}))
      }
    } catch (error) {
      then(new FailResponse({data,error}))
    }

  },
  /**
   * 创建文件夹
   * @param {*} data 
   * @param {*} then 
   */
  init_env (data, then) {
    try {
      const client = sshClients[data.serverId]
      // rm -rf /home/admin/nginx\n
      client.shell(`
        fuser -k 8080/tcp\n
        rm -rf /home/admin/nginx-1.19.1.tar.gz\n
        rm -rf /home/admin/nginx-1.19.5.green.tar.gz\n
        su admin\n
        cp -f /home/admin/nginx/nginx-service/conf/nginx.conf /home/admin/nginx.conf.old.bak \n
        cp -f /home/admin/nginx/conf/nginx.conf /home/admin/nginx.conf.1.19.1.bak \n
        exit \n
        rm -rf /home/admin/nginx \n
        su admin\n
      `, () => {
      }, (remoteData) => {
        then(new Response({ data, remoteData }))
      })
    } catch (error) {
      then(new FailResponse({data,error}))
    }
  },
  /**
   * 上传nginx
   * @param {*} data 
   * @param {*} then 
   */
  upload_nginx (data, then) {
    try {
      const client = sshClients[data.serverId]
      const localPath = path.join(process.cwd(), 'src/resources/nginx-1.19.5.green.tar.gz')
      const remotePath = '/home/admin/nginx-1.19.5.green.tar.gz'
      client.uploadFile(localPath, remotePath, (remoteData) => {
        then(new Response({ data, remoteData }))
      })
    } catch (error) {
      then(new FailResponse({data,error}))
    }
  },
  /**
   * 上传nginx子中心配置文件
   * @param {*} data 
   * @param {*} then 
   */
  upload_child_config (data, then) {
    try {
      const client = sshClients[data.serverId]
      const localPath = path.join(process.cwd(), 'src/resources/nginx-child.conf')
      const remotePath = '/home/admin/nginx/conf/nginx.conf'
      client.uploadFile(localPath, remotePath, (remoteData) => {
        then(new Response({ data, remoteData }))
      })
    } catch (error) {
      then(new FailResponse({data,error}))
    }
  },
  /**
   * 解压nginx
   * @param {*} data 
   * @param {*} then 
   */
  unzip_nginx (data, then) {
    try {
      const client = sshClients[data.serverId]
      client.shell(`
      su admin\n
      cd /home/admin/\n
      tar -xvf nginx-1.19.5.green.tar.gz\n
      cp -f /home/admin/nginx.conf.old.bak /home/admin/nginx/conf/nginx.conf\n
      cp -f /home/admin/nginx.conf.1.19.1.bak /home/admin/nginx/conf/nginx.conf\n
      `, () => {
      }, (remoteData) => {
        then(new Response({ data, remoteData }))
      })
    } catch (error) {
      then(new FailResponse({data,error}))
    }
  },
  /**
   * 启动nginx
   * @param {*} data 
   * @param {*} then 
   */
  start_nginx (data, then) {
    try {
      const client = sshClients[data.serverId]
      client.shell('su admin\n/home/admin/nginx/sbin/nginx -s stop\n/home/admin/nginx/sbin/nginx\n', () => {
      }, (remoteData) => {
        then(new Response({ data, remoteData }))
      })
    } catch (error) {
      then(new FailResponse({data,error}))
    }
  },
  /**
   * nginx 日志拆分
   * @param {*} data 
   * @param {*} then 
   */
  split_nginx_log (data, then) {
    try {
      const client = sshClients[data.serverId]
      client.shell('echo -e \'* 0 * * * sh /home/admin/nginx/backup_nginx_log.sh\' >> /var/spool/cron/root\nservice crond reload\n', () => {
      }, (remoteData) => {
        then(new Response({ data, remoteData }))
      })
    } catch (error) {
      then(new FailResponse({data,error}))
    }
  },

   /**
   * 复制旧nginx配置文件到新nginx配置目录
   * @param {*} data 
   * @param {*} then 
   */
  copy_old_nginx_config (data, then) {
    try {
      const client = sshClients[data.serverId]
      client.shell(`
        cp /home/admin/nginx/nginx-service/conf/nginx.conf /home/admin/conf/nginx.conf
      `, () => {
      }, (remoteData) => {
        then(new Response({ data, remoteData }))
      })
    } catch (error) {
      then(new FailResponse({data,error}))
    }
  }


  

  // echo -e '* 0 * * * sh /home/admin/nginx/backup_nginx_log.sh' >> /var/spool/cron/root
  // service crond reload
}
export default {
  moduleName: 'server',
  module: control
}